package com.bdqn.controller;

import com.bdqn.pojo.SysUser;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

// public class HelloController extends AbstractController {
//public class HelloController implements Controller {
@Controller
@RequestMapping(value = "/hello")
public class HelloController {
    private Logger logger = Logger.getLogger(HelloController.class);

    @RequestMapping(value = "/hello")
    public String hello(@RequestParam(required = false) String realName) throws Exception {
        logger.info("你好" + realName + "欢迎来到Spring MVC课堂。");
        System.out.println("你好" + realName + "欢迎来到Spring MVC课堂。");
//        System.out.println("Spring MVC 框架搭建成功。");
        //跳转到hello 页面
        return "hello";
    }

    @RequestMapping(value = "/hello2")
    public String hello2(Model model, @RequestParam(required = false) String realName) throws Exception {
        logger.info("你好" + realName + "欢迎来到Spring MVC课堂。");
        System.out.println("Hello控制器下的另一个接口！");
        model.addAttribute("realName", realName);
        model.addAttribute(realName);//根据string调用不赋值保存非
        SysUser sysUser = new SysUser();
        sysUser.setRealName(realName);
        model.addAttribute("currentUser", sysUser);
        model.addAttribute(sysUser);
        //跳转到hello2 页面
        return "hello2";
    }

    @RequestMapping(value = "/hello3")
    public ModelAndView hello3(@RequestParam String realName) throws Exception {
        logger.info("你好【" + realName + "】欢迎来到Spring MVC课堂。");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("realName", realName);
        modelAndView.setViewName("hello");
        return modelAndView;
    }

    @RequestMapping(value = "/hello4")
    public String hello4(Map<String,Object> map,@RequestParam String realName) throws Exception{
        logger.info("你好【" + realName + "】欢迎来到Spring MVC课堂。");
        map.put("realName",realName);
        return "hello";
    }
//public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
////       创建一个模型和视图
//    ModelAndView modelAndView = new ModelAndView();
////        封装对象，放在ModelAndView中
//    modelAndView.addObject("msg","HelloSpringMVC");
//
////        封装要跳转的视图，放在ModelAndView中
//    modelAndView.setViewName("hello");    //: /WEB-INF/jsp/hello.jsp
//    return modelAndView;
//}
//
//     @Override
//     public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
//         return null;
//     }
}
